<template>
  <div style="height: 90%; width: 100%">
    <div id="princiEchart" style="height: 100%; width: 100%"></div>
  </div>
</template>

 <script>
export default {
  name: "princiEchart",
  props: {
    principal: {
      type: Object,
      default: () => {
        return {};
      },
    },
  },
  data() {
    return {
        max: 0
    }},
  watch: {
    principal(val) {
        let max = val.indexValueHb.reduce((a,b) => {
            return Number(b)>Number(a) ? b:a
        })
        this.max = max
        this.getPie(val);
    },
  },
  mounted() {

  },
  methods: {
    getPie(val) {
      var myChart = this.$echarts.init(document.getElementById("princiEchart"));
      var option = {
        tooltip: {
          trigger: "axis",
          axisPointer: {
            // 坐标轴指示器，坐标轴触发有效
            type: "shadow", // 默认为直线，可选为：'line' | 'shadow'
          },
        },
        legend: {
          data: ["环比", "台阶收入"],
          bottom: 0
        },
        grid: {
          left: "10px",
          right: "10px",
          bottom: "50px",
          containLabel: true,
        },
        xAxis: {
          data: val.cycleValue,
          type: "category",
          boundaryGap: true,
        },
        yAxis: [{
          type: "value",
          max: this.max,
          splitLine: {
            show: false,
          },
          axisLabel: {
            show: false,
          },
          axisTick: {
            //y轴刻度线
            show: false,
          },
          axisLine: {
            //y轴
            show: false,
          },
        },{
          splitLine: {
            show: false,
          },
          axisLabel: {
            show: false,
          },
          axisTick: {
            //y轴刻度线
            show: false,
          },
          axisLine: {
            //y轴
            show: false,
          },
        }],
        series: [
          {
            name: "环比",
            type: "bar",
            yAxisIndex: 0,
            data: val.indexValueHb,
            barWidth : 20,//柱图宽度
            itemStyle: {
              normal: {
                color: function(params) {
                    if (params.data <0 ) {
                        return '#C0504D'
                    } else {
                        return '#4BACC6'
                    }
                },
                label: {
                  show: true,
                  position: "inside",
                  textStyle: {
                    color: "#404040",
                  },
                },
              },
            },
          },
          {
            name: "台阶收入",
            type: "line",
            data: val.indexValue,
            yAxisIndex: 1,
            itemStyle: {
              normal: {
                color: "#C0504D",
                label: {
                  show: true,
                  position: "top",
                  textStyle: {
                    color: "red",
                  },
                },
              },
            },
          },
        ],
      };
      myChart.setOption(option);
    },
  },
};
</script>

 <style>
</style>
